package com.yuand.controller;

import com.yuand.entity.SysUser;
import com.yuand.service.SysUserService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author yuandong
 * @date 2021/8/9 13:33
 * 用户注册Controller
 */
@Controller
public class RegisterController {

    @Autowired
    private SysUserService userService;

    /**
     * 进入注册页面
     * @return
     */
    @RequestMapping({"/", "/register.html"})
    public String toRegister(){
        return "register";
    }


    /**
     * 注册提交页面
     * @param sysUser
     * @return
     */
    @RequestMapping("/user/register")
    public String register(SysUser sysUser){

        //SecureRandomNumberGenerator类随机方法创建盐，进行两次加密，加密算法用md5
        String salt = new SecureRandomNumberGenerator().nextBytes().toString();
        //SimpleHash类使用md5加密算法加密两次，把盐加进去，生成新的密码
        String encodedPassword = new SimpleHash("md5", sysUser.getPassword(), salt, 2).toString();
        sysUser.setSalt(salt);
        sysUser.setPassword(encodedPassword);
        this.userService.insert(sysUser);
        return "login";     //注册成功挑战登录页面
    }
}
